package com.itheima.Object.test1;

public class ObjectDemo {
    public static void main(String[] args) {
        Student stu1 = new Student("杨世兵",25);
        Student stu2 = new Student("杨世兵",25);
        //第一种是应为地址值不同
        System.out.println(stu1 == stu2);//false
        //第二种比较的是内容按理说应该是true但是在这其中object类中的equals方法也是通过==号判断的
        //stu1调用的方法说明就去Student类当中进行寻找,但是没有就只能去父类object寻找,可以看到点击ctrl equals可以看出来也是==号比较
        System.out.println(stu1.equals(stu2));//false
        //但是当重写了equals方法之后在比较就是内容了
        /*
         public boolean equals(Object obj) {
        return (this == obj);
    }
         */
    }

}


//创建一个学生类

class Student{
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        if (age != student.age) return false;
        return name != null ? name.equals(student.name) : student.name == null;
    }

}